package ta.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import ta.dao.UserDao;
import ta.pojo.User;
import ta.utils.PageInfo;

@WebServlet("/admin/UserServlet")
public class UserServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {		
		String type = request.getParameter("type");
		switch (type)
		{
		case "add":
             add(request, response);
			break;

		case "del":
              del(request, response);
			break;

		case "edit":
            edit(request, response);
			break;

		case "getlist":
            getList(request, response);
			break;

		case "getbyid":
           getById(request, response);
			break;

		case "repwd":
             repwd(request, response);
			break;
		}

	}

	// 添加用户
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, String[]> map = request.getParameterMap();
		User user = new User();
		try
		{
			BeanUtils.populate(user, map);
		} catch (IllegalAccessException | InvocationTargetException e)
		{
			e.printStackTrace();
		}
		UserDao dao = new UserDao();
		dao.register(user);
		// 跳转到管理页面
		response.sendRedirect("UserServlet?type=getlist&page=1");
	}

	// 删除
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userid = request.getParameter("userid");
		UserDao dao = new UserDao();
		dao.del(userid);
		response.sendRedirect("UserServlet?type=getlist&page=1");
	}

	// 修改
	protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, String[]> map = request.getParameterMap();
		User user = new User();
		try
		{
			BeanUtils.populate(user, map);
		} catch (IllegalAccessException | InvocationTargetException e)
		{
			e.printStackTrace();
		}
		UserDao dao = new UserDao();
		dao.edit(user);
		response.sendRedirect("UserServlet?type=getlist&page=1");
	}

	// 搜索和分页查询
	protected void getList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String keyword = request.getParameter("keyword");// null--""
		String currentpage = request.getParameter("page");

		int page1 = currentpage == null ? 1 : Integer.valueOf(currentpage);
		keyword = keyword == null ? "" : keyword;
		UserDao dao = new UserDao();
		PageInfo<User> pb = new PageInfo<User>();
		pb.setCurrentpage(page1);
		dao.getAll(keyword, pb);

		request.setAttribute("pb", pb);
		request.setAttribute("keyword", keyword);
		request.getRequestDispatcher("user/list.jsp").forward(request, response);

	}

	protected void getById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String userid = request.getParameter("userid");
		UserDao dao = new UserDao();
		User user = dao.getById(userid);
		request.setAttribute("user", user);
		request.getRequestDispatcher("user/edit.jsp").forward(request, response);
	}

	protected void repwd(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String username = (String) request.getSession().getAttribute("loginame");
		String userpwd = request.getParameter("userpwd");
		String newpwd = request.getParameter("newpwd");
		UserDao dao = new UserDao();
		User user = dao.getByName(username);
		if (userpwd.equals(user.getUserpwd()))
		{
			user.setUserpwd(newpwd);
			dao.edit(user);
		}
		response.sendRedirect("UserServlet?type=getlist&page=1");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
